//
//  DatePickerViewController.m
//  Test
//
//  Created by chenjs on 2017/8/14.
//  Copyright © 2017年 dave. All rights reserved.
//
//source:https://github.com/xiaozhuxiong121/PGDatePicker

#import "DatePickerViewController.h"
#import <HandyFrame/UIView+LayoutMethods.h>
#import "PGDatePicker.h"

static NSInteger  const year = 1;
static NSInteger  const yearMonth = 2;
static NSInteger  const yearMonthDay = 3;
static NSInteger  const hourMinute = 4;
static NSInteger  const ymwhm = 5;
static NSInteger  const withTitle = 6;
static NSInteger  const setStyle = 7;

#define buttonBGColor [UIColor colorWithRed:0 green:122 / 255.0 blue:255 / 255.0 alpha:1.0]

@interface DatePickerViewController () <PGDatePickerDelegate>

@property (nonatomic, strong) UIButton *yearButton;

@property (nonatomic, strong) UIButton *yearAndMothBT;

@property (nonatomic, strong) UIButton *ymdBT;

@property (nonatomic, strong) UIButton *hourAndMinuteBT;

@property (nonatomic, strong) UIButton *ymwhmBT;

@property (nonatomic, strong) UIButton *withTitleBT;

@property (nonatomic, strong) UIButton *setStyleBT;

@end

@implementation DatePickerViewController

#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.yearButton];
    [self.view addSubview:self.yearAndMothBT];
    [self.view addSubview:self.ymdBT];
    [self.view addSubview:self.hourAndMinuteBT];
    [self.view addSubview:self.ymwhmBT];
    [self.view addSubview:self.withTitleBT];
    [self.view addSubview:self.setStyleBT];
}

- (void)viewDidLayoutSubviews {
    [self.yearButton setCt_width:100.0];
    [self.yearButton setCt_height:40.0];
    [self.yearButton topInContainer:100.0 shouldResize:NO];
    [self.yearButton leftInContainer:20.0 shouldResize:NO];
    
    [self.yearAndMothBT setCt_width:100.0];
    [self.yearAndMothBT setCt_height:40.0];
    [self.yearAndMothBT topInContainer:100.0 shouldResize:NO];
    [self.yearAndMothBT rightInContainer:20.0 shouldResize:NO];
    
    [self.ymdBT setCt_width:100.0];
    [self.ymdBT setCt_height:40.0];
    [self.ymdBT topInContainer:160.0 shouldResize:NO];
    [self.ymdBT leftInContainer:20.0 shouldResize:NO];
    
    [self.hourAndMinuteBT setCt_width:100.0];
    [self.hourAndMinuteBT setCt_height:40.0];
    [self.hourAndMinuteBT topInContainer:160.0 shouldResize:NO];
    [self.hourAndMinuteBT rightInContainer:20.0 shouldResize:NO];
    
    [self.ymwhmBT setCt_width:100.0];
    [self.ymwhmBT setCt_height:40.0];
    [self.ymwhmBT topInContainer:220.0 shouldResize:NO];
    [self.ymwhmBT leftInContainer:20.0 shouldResize:NO];
    
    [self.withTitleBT setCt_width:100.0];
    [self.withTitleBT setCt_height:40.0];
    [self.withTitleBT topInContainer:220.0 shouldResize:NO];
    [self.withTitleBT rightInContainer:20.0 shouldResize:NO];
    
    [self.setStyleBT setCt_width:100.0];
    [self.setStyleBT setCt_height:40.0];
    [self.setStyleBT topInContainer:280.0 shouldResize:NO];
    [self.setStyleBT leftInContainer:20.0 shouldResize:NO];
}

#pragma PGDatePickerDelegate

- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {
    NSLog(@"dateComponents = %@", dateComponents);
}

#pragma mark - event response

- (void)buttonTaped:(UIButton *)sender {
    switch (sender.tag) {
        case year:
        {
            PGDatePicker *datePicker = [[PGDatePicker alloc]init];
            datePicker.delegate = self;
            [datePicker show];
            datePicker.datePickerMode = PGDatePickerModeYear;
        }
            break;
        case yearMonth:
        {
            PGDatePicker *datePicker = [[PGDatePicker alloc]init];
            datePicker.delegate = self;
            [datePicker show];
            datePicker.datePickerMode = PGDatePickerModeYearAndMonth;
        }
            break;
        case yearMonthDay:
        {
            PGDatePicker *datePicker = [[PGDatePicker alloc]init];
            datePicker.delegate = self;
            [datePicker show];
            datePicker.datePickerMode = PGDatePickerModeDate;
        }
            break;
        case hourMinute:
        {
            PGDatePicker *datePicker = [[PGDatePicker alloc]init];
            datePicker.delegate = self;
            [datePicker show];
            datePicker.datePickerMode = PGDatePickerModeTime;
        }
            break;
        case ymwhm:
        {
            PGDatePicker *datePicker = [[PGDatePicker alloc]init];
            datePicker.delegate = self;
            [datePicker show];
            datePicker.datePickerMode = PGDatePickerModeDateAndTime;
        }
            break;
        case withTitle:
        {
            PGDatePicker *datePicker = [[PGDatePicker alloc]init];
            datePicker.delegate = self;
            [datePicker show];
            datePicker.titleLabel.text = @"PGDatePicker";
            datePicker.datePickerMode = PGDatePickerModeDate;
        }
            break;
        case setStyle:
        {
            PGDatePicker *datePicker = [[PGDatePicker alloc]init];
            datePicker.delegate = self;
            [datePicker show];
            datePicker.titleLabel.text = @"PGDatePicker";
            //设置线条的颜色
            datePicker.lineBackgroundColor = [UIColor redColor];
            //设置选中行的字体颜色
            datePicker.titleColorForSelectedRow = [UIColor redColor];
            //设置未选中行的字体颜色
            datePicker.titleColorForOtherRow = [UIColor blackColor];
            //设置取消按钮的字体颜色
            [datePicker.cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            //设置确定按钮的字体颜色
            [datePicker.confirmButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            datePicker.datePickerMode = PGDatePickerModeDate;
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - getter

- (UIButton *)yearButton {
    if (_yearButton == nil) {
        _yearButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _yearButton.tag = year;
        [_yearButton addTarget:self action:@selector(buttonTaped:) forControlEvents:UIControlEventTouchUpInside];
        [_yearButton setTitle:@"年份" forState:UIControlStateNormal];
        [_yearButton setBackgroundColor:buttonBGColor];
    }
    return _yearButton;
}

- (UIButton *)yearAndMothBT {
    if (_yearAndMothBT == nil) {
        _yearAndMothBT = [UIButton buttonWithType:UIButtonTypeCustom];
        _yearAndMothBT.tag = yearMonth;
        [_yearAndMothBT addTarget:self action:@selector(buttonTaped:) forControlEvents:UIControlEventTouchUpInside];
        [_yearAndMothBT setTitle:@"年月" forState:UIControlStateNormal];
        [_yearAndMothBT setBackgroundColor:buttonBGColor];
    }
    return _yearAndMothBT;
}

- (UIButton *)ymdBT {
    if (_ymdBT == nil) {
        _ymdBT = [UIButton buttonWithType:UIButtonTypeCustom];
        _ymdBT.tag = yearMonthDay;
        [_ymdBT addTarget:self action:@selector(buttonTaped:) forControlEvents:UIControlEventTouchUpInside];
        [_ymdBT setTitle:@"年月日" forState:UIControlStateNormal];
        [_ymdBT setBackgroundColor:buttonBGColor];
    }
    return _ymdBT;
}

- (UIButton *)hourAndMinuteBT {
    if (_hourAndMinuteBT == nil) {
        _hourAndMinuteBT = [UIButton buttonWithType:UIButtonTypeCustom];
        _hourAndMinuteBT.tag = hourMinute;
        [_hourAndMinuteBT addTarget:self action:@selector(buttonTaped:) forControlEvents:UIControlEventTouchUpInside];
        [_hourAndMinuteBT setTitle:@"时分" forState:UIControlStateNormal];
        [_hourAndMinuteBT setBackgroundColor:buttonBGColor];
    }
    return _hourAndMinuteBT;
}

- (UIButton *)ymwhmBT {
    if (_ymwhmBT == nil) {
        _ymwhmBT = [UIButton buttonWithType:UIButtonTypeCustom];
        _ymwhmBT.tag = ymwhm;
        [_ymwhmBT addTarget:self action:@selector(buttonTaped:) forControlEvents:UIControlEventTouchUpInside];
        [_ymwhmBT setTitle:@"年月周 时分" forState:UIControlStateNormal];
        [_ymwhmBT setBackgroundColor:buttonBGColor];
    }
    return _ymwhmBT;
}

- (UIButton *)withTitleBT {
    if (_withTitleBT == nil) {
        _withTitleBT = [UIButton buttonWithType:UIButtonTypeCustom];
        _withTitleBT.tag = withTitle;
        [_withTitleBT addTarget:self action:@selector(buttonTaped:) forControlEvents:UIControlEventTouchUpInside];
        [_withTitleBT setTitle:@"带标题" forState:UIControlStateNormal];
        [_withTitleBT setBackgroundColor:buttonBGColor];
    }
    return _withTitleBT;
}

- (UIButton *)setStyleBT {
    if (_setStyleBT == nil) {
        _setStyleBT = [UIButton buttonWithType:UIButtonTypeCustom];
        _setStyleBT.tag = setStyle;
        [_setStyleBT addTarget:self action:@selector(buttonTaped:) forControlEvents:UIControlEventTouchUpInside];
        [_setStyleBT setTitle:@"带标题" forState:UIControlStateNormal];
        [_setStyleBT setBackgroundColor:buttonBGColor];
    }
    return _setStyleBT;
}
@end
